<template>
  <div>
    <el-button type="primary" size="default" icon="ele-Plus" @click="toAddSpu"
      >添加SPU</el-button
    >
    <el-table
      v-loading="loading"
      border
      stripe
      style="margin: 20px 0"
      :data="spuList"
    >
      <el-table-column type="index" width="80" label="序号" />
      <el-table-column label="Spu名称" prop="spuName"></el-table-column>
      <el-table-column label="Spu描述" prop="description"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            size="small"
            icon="ele-Plus"
            @click="toAddSku(row)"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="ele-Edit"
            @click="toUpdateSpu(row)"
          ></el-button>
          <el-button type="info" size="small" icon="ele-Warning"></el-button>
          <el-button type="danger" size="small" icon="ele-Delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:page-size="pageSize"
      :page-count="pageCount"
      :pager-count="5"
      :page-sizes="[3, 5, 7, 9]"
      v-model:current-page="currentPage"
      layout=" prev, pager, next, sizes"
    />
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  name: "SpuList",
});
</script>

<script setup lang="ts">
import { ref, watch } from "vue";
import { storeToRefs } from "pinia";
import { ElMessage } from "element-plus";
import { reqSpuList, type spuItemType, type spuListType } from "@/api/spu";
import useCategoryStore from "@/stores/category";
//拿到三级分类的信息
const categoryStore = useCategoryStore();
const { category3Id } = storeToRefs(categoryStore);

//定义页码需要的数据
//1.每页数量
const pageSize = ref(3);
//2.总页数
const pageCount = ref(0);
//3.当前页
const currentPage = ref(1);
// 4.table是否加载中
const loading = ref(false);

//接收切换type的自定义事件
const emit = defineEmits<{
  (event: "changeType", type: number, row?: spuItemType): void;
}>();
//切换组件的点击回调函数
const toAddSpu = () => {
  emit("changeType", 1);
};
const toAddSku = (row:spuItemType) => {
  emit("changeType", 2,row);
};
const toUpdateSpu = (row: spuItemType) => {
  emit("changeType", 1, row);
};
//定义spu列表的初始值
const spuList = ref<spuListType>([]);

//封装获取spu列表的请求
const getSpuList = async () => {
  try {
    const result = await reqSpuList(
      currentPage.value,
      pageSize.value,
      category3Id.value as number
    );
    //把总页数和列表属性赋值给自己的数据
    spuList.value = result.records;
    pageCount.value = result.pages;

    loading.value = false;
  } catch (e: any) {
    ElMessage.error(e.message);
  }
};
//监听3id的改变，发送请求
watch(category3Id, (newVal) => {
  if (!newVal) return;
  loading.value = true;
  //发送初始化请求
  getSpuList();
},{immediate:true});

//监听当前页和每页条数 重新发送请求
watch([currentPage, pageSize], () => {
  loading.value = true;
  //发送初始化请求
  getSpuList();
});
</script>

<style scoped></style>
